package com.bsg.upm.controller;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.MonitorComponentService;

@Controller
@RequestMapping("/v1.0/monitor/component")
public class MonitorComponentController {
	@Resource
	private MonitorComponentService monitorComponentService;

	@RequestMapping(value = "system",method = RequestMethod.GET)
	@ResponseBody
	public RespJson getAllComponent(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		try {
			String stieId = (String) paramMap.get("site");

			respJson = monitorComponentService.getAllComponent(stieId);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}

	@RequestMapping(value = "hosts", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getAllHostComponent(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		try {
			
			String clusterId =null;
			String siteId =null;
			
			if (paramMap.containsKey("cluster")) {
				clusterId = (String) paramMap.get("cluster");
			}
			
			if (paramMap.containsKey("site")) {
				siteId = (String) paramMap.get("site");
			}
		
			respJson = monitorComponentService.getAllHostComponent(siteId,clusterId);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}
	
	@RequestMapping(value = "host/containers", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getAllHostContainersStatus(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		try {
			String hostId = (String) paramMap.get("host");
			respJson = monitorComponentService.getAllHostContainersStatus(hostId);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}
	 
}
